/*
    1. 获取系统时间
    2. 判断文件是否存在
    3. 获取文件所在的目录路径
    4. 创建目录
*/
#pragma once
#include <fstream>
#include <vector>
#include <iostream>
#include <string>
#include <ctime>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

namespace XKL
{
    namespace Util
    {
        class Date
        {
        public:
            // 获取当前时间戳
            static size_t Current()
            {
                return (size_t)time(nullptr);
            }
        };

        class File
        {
        public:
            // 文件存在性判断
            static bool Exist(const std::string &filename)
            {
                // Linux 本地方案，不具备跨平台性
                // return access(filename.c_str(), F_OK);

                // 读取文件信息
                // 如果读取成功的话，就代表文件存在
                struct stat buf;
                if (stat(filename.c_str(), &buf) == 0)
                {
                    return true;
                }

                return false;
            }
            // 配置文件初始化
            static void BufferInit(size_t &buffer_size, size_t &threshold_size, size_t &increment_size)
            {
                std::vector<int> buf;
                size_t size = 1024 * 1024;
                std::ifstream inFile;
                std::string tmp;
                std::string filename = "logfile.ini";
                if (!XKL::Util::File::Exist(filename))
                {
                    // 不存在就创建
                    std::ofstream outFile;
                    outFile.open(filename);
                    outFile << "[Buffer]" << std::endl;
                    outFile << "DEFAULT_BUFF_SIZE=4" << std::endl;
                    outFile << "THRESHOLD_BUFF_SIZE=20" << std::endl;
                    outFile << "INCREMENT_BUFF_SIZE=2";
                    outFile.close();
                }

                inFile.open(filename);
                while (true)
                {
                    if (inFile.eof())
                        break;
                    inFile >> tmp;
                    size_t pos = tmp.rfind('=');
                    if (pos != std::string::npos)
                        buf.push_back(std::stoi(tmp.substr(pos + 1)));
                }
                inFile.close();

                buffer_size = buf[0] * size;
                threshold_size = buf[1] * size;
                increment_size = buf[2] * size;
            }
            // 获取文件所在目录路径
            static std::string Path(const std::string &filename)
            {
                if (filename.empty())
                    return ".";

                auto pos = filename.find_last_of("/\\");
                if (pos == std::string::npos)
                    return ".";

                return filename.substr(0, pos + 1);
            }
            // 创建目录
            static void CreateDirectory(const std::string &filename)
            {
                if (filename.empty() || Exist(filename))
                    return;

                int pos = 0, index = 0;
                // 循环判断路径合法性，并创建目录
                while (index < filename.size())
                {
                    pos = filename.find_first_of("/\\", index);
                    if (pos == std::string::npos)
                    {
                        mkdir(filename.c_str(), 0777);
                        return;
                    }

                    std::string parent = filename.substr(0, pos + 1);
                    if (Exist(parent))
                    {
                        index = pos + 1;
                        continue;
                    }

                    mkdir(parent.c_str(), 0777);
                    index = pos + 1;
                }
            }
        };
    }
}